package com.briup.bms.service.impl;

import com.briup.bms.bean.Department;
import com.briup.bms.bean.SCustomer;
import com.briup.bms.common.exceptions.BmsServiceException;
import com.briup.bms.common.utils.EntityUtils;
import com.briup.bms.common.utils.ResultCode;
import com.briup.bms.dao.SCustomerMapper;
import com.briup.bms.service.SCustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author: Paocha
 * @date: 2021-11-23-14:42
 */
@Service
public class CustomerServiceImpl implements SCustomerService {

    private final SCustomerMapper customerMapper;

    public CustomerServiceImpl(SCustomerMapper customerMapper) {
        this.customerMapper = customerMapper;
    }


    @Override
    public PageInfo<SCustomer> findByConditon(int pageNum, int pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        name = (name==null||"".equals(name))?name:"%"+name+"%";
        List<SCustomer> customers=customerMapper.selectByCondition(name);
        return new PageInfo<>(customers);
    }

    @Override
    public PageInfo<SCustomer> findByPageHelper(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SCustomer> customers=customerMapper.selectAll();
        return new PageInfo<>(customers);
    }

    @Override
    public void saveOrUpdate(SCustomer customer) {


        if(customer.getId()!=null) {
            //更新
            customerMapper.updateByPrimaryKey(customer);
        }else {
            //保存
            customerMapper.insert(customer);
        }
    }

    @Override
    public void batchDeleteCustomer(Set<Integer> customerIds) {
        customerMapper.batchDelete(customerIds);
    }

    @Override
    public void deleteCustomer(Integer customerId) {
        SCustomer customer=customerMapper.selectByPrimaryKey(customerId);
        if (ObjectUtils.isEmpty(customer)){
            throw new BmsServiceException(ResultCode.DATA_NOT_EXIST,"数据不存在");
        }
        customerMapper.deleteByPrimaryKey(customerId);
    }
}
